package com.ruoyi.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.VendorConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.ValidatorUtils;
import com.ruoyi.system.domain.SysVendor;
import com.ruoyi.system.service.ISysVendorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@Controller
@RestController
@RequestMapping("/vendor")
public class SysVendorController extends BaseController {

    @Autowired
    private ISysVendorService sysVendorService;
        //根据oid号查询供应商信息
        @GetMapping("/find/{oid}")
        //@ResponseBody
        public SysVendor selectVendorByOid(@PathVariable("oid") Integer oid){
            return sysVendorService.selectVendorByOid(oid);
        }
        //查询供应商信息列表（所有）(带正序条件)
        @PostMapping("/find/all")
        //@ResponseBody
        public List<SysVendor> selectVendorList(@RequestBody SysVendor sysVendor){
            return sysVendorService.selectVendorList(sysVendor);
        }

        //查询供应商信息列表（所有）(带倒序条件)
        @PostMapping("/find/allDesc")
        //@ResponseBody
        public List<SysVendor> selectVendorListDesc(@RequestBody SysVendor sysVendor){
        return sysVendorService.selectVendorListDesc(sysVendor);
    }

        //插入一条供应商信息（json表单）
        @PostMapping("/insert/list")
        //@ResponseBody
        public R insertVendorList(@RequestBody SysVendor sysVendor){
//            if (sysVendor.getStatus().equals("1")){
//                return R.error("你的供应商账号被禁了");
//            }
            ValidatorUtils.validateEntity(sysVendor, SysVendor.AddGroup.class);
            ValidatorUtils.validateEntity(sysVendor);
            //return sysVendorService.insertVendorMapper(sysVendor);
            return toAjax(sysVendorService.insertVendorMapper(sysVendor));
        }

        //根据oid删除供应商信息
        @GetMapping("/delete/{oid}")
        //@ResponseBody
        public R deleteVendorMapper(@PathVariable Integer oid){
            return toAjax(sysVendorService.deleteVendorMapper(oid));
        }

        //根据oid组编号批量删除供应商信息（a,b,c）
        @GetMapping("/deletes")
        //@ResponseBody
        public R deleteVendorsMapper(String oids) throws Exception {
            return toAjax(sysVendorService.deleteVendorsMapper(oids));
        }

        //根据oid号更新供应商信息
        @PostMapping("/update")
        //@ResponseBody
        public R updateVendorMapper(@RequestBody SysVendor sysVendor){
            ValidatorUtils.validateEntity(sysVendor, SysVendor.AddGroup.class);
            ValidatorUtils.validateEntity(sysVendor);
            return toAjax(sysVendorService.updateVendorMapper(sysVendor));
        }

        //非空判断，供给供应商信息更新使用（update专用）
        @PostMapping("/checkNotEmpty")
        //@ResponseBody
        public int checkMessageNotEmpty(@RequestBody String message){
            JSONObject jsonObject = JSON.parseObject(message);
            String mess = jsonObject.getString("message");
            if (mess.isEmpty()||mess==null||mess==""){
                return VendorConstants.MESSAGE_NULL;
            }
            return VendorConstants.MESSAGE_NOT_NULL;
        }

        //模糊搜索功能，可以根据所有字段信息定制模糊匹配供应商信息(带正序条件)
        @PostMapping("/search")
        //@ResponseBody
        public List<SysVendor> searchVendor(@RequestBody SysVendor sysVendor){
            return sysVendorService.searchVendor(sysVendor);
        }

        //模糊搜索功能，可以根据所有字段信息定制模糊匹配供应商信息(带倒序条件)
        @PostMapping("/searchDesc")
        //@ResponseBody
        public List<SysVendor> searchVendorDesc(@RequestBody SysVendor sysVendor){
            return sysVendorService.searchVendorDesc(sysVendor);
        }

        //唯一性查询（同模糊搜索，去掉时间节点）
        @PostMapping("/checkUnique")
        public boolean checkUnique(@RequestBody SysVendor sysVendor){
            //若返回请求统计数为0，true通过
            if(sysVendorService.checkUnique(sysVendor) == 0){
                return true;
            //若返回请求统计数不为0，false未通过
            }else{
                return false;
            }
        }

        //供应商信息审核功能
        @PostMapping("/effectivePassed")
        public R effectivePassed(@RequestBody SysVendor sysVendor){
            String mess = sysVendorService.selectVendorByOid(sysVendor.getOid()).getVendorNum();
            //非空判断
            if (sysVendor.getVendorNum().isEmpty()||sysVendor.getVendorNum()==null){
                return R.error("request refused！！，请输入供应商代码");
            }
            else if (sysVendor.getVendorNum().equals(mess)){
                return R.error("正式供应商代码和临时代码重复");
            }
            else{
                return toAjax(sysVendorService.effectivePassed(sysVendor));
            }
        }
}
